<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Material extends Model
{
    /**
     * @var string
     */
    protected $table='material';
    /**
     * @var
     */
    protected $materialModel;

    /**
     * Material constructor.
     */

    public function __construct()
    {
        $this->materialModel = DB::table($this->table);
    }

    /**
     * @param $params
     * @return array
     */

    public function getList($params){
        $data=[];
        if($params['getfield']){
            $data['total'] = $this->materialModel->where($params['where'])->count();
            $data['row'] = $this->materialModel->where($params['where'])->orderByDesc($params['order'])->select($params['field'])->skip(($params['page']-1)*$params['pagesize'])->take($params['pagesize'])->get()->toArray();
        }else{
            $data['row'] = $this->materialModel->where($params['where'])->orderByDesc($params['order'])->get($params['field']);
        }
        return $data;

    }

    /**
     * @param $data
     * @return bool
     */
    public function add($data){
      $data['updated_at'] = date("Y-m-d H:i:s");
      return  $this->materialModel->insert($data);
    }

    /**
     * @param $data
     * @return Model|\Illuminate\Database\Query\Builder|null|object
     */
    public function findOne($data){
        return $this->materialModel->where($data['where'])->first();
    }
    public function updateData($data){
        return $this->materialModel->where($data['where'])->update($data['field']);
    }
}
